<template>
  <div class="main-box">
    <van-nav-bar
      :title="useCode ? '手机登录注册' : '密码登录'"
      left-text="返回"
      :right-text="useCode ? '密码登录' : '手机登录注册'"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="logo"></div>
    <van-form @submit="loginPwd" v-if="!useCode">
      <van-field
        class="login-form"
        v-model="formModel.phone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="formModel.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <br /><van-button round block type="info">登录</van-button>
        <br /><van-button
          round
          block
          plain
          hairline
          type="info"
          native-type="button"
          @click="onClickRight"
          >注册账号</van-button
        >
      </div>
    </van-form>
    <van-form @submit="login" v-else>
      <van-field
        class="login-form"
        v-model="formModel.phone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="formModel.password"
        type="password"
        name="验证码"
        label="验证码"
        placeholder="验证码"
        :rules="[{ required: true, message: '请填写验证码' }]"
      />
      <div style="margin: 16px">
        <br /><van-button
          round
          block
          plain
          hairline
          type="info"
          native-type="button"
          @click="getCode"
          >发送验证码</van-button
        >
        <br /><van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLoginService, userPwdLoginService } from "@/api/userAPI.js";
import { Notify, Toast } from "vant";
import store from "@/store";

export default {
  name: "LoginPage",
  data() {
    return {
      // 登录/注册 表单
      formModel: {
        phone: "",
        password: "",
        code: "",
      },
      // 判断是在 验证码登录/注册（false） or 密码登录（true）
      useCode: false,
      router: {},
    };
  },
  created() {},
  methods: {
    async loginPwd() {
      const res = await userPwdLoginService(this.formModel);
      // 保存token
      this.$store.commit("mSetTokenInfo", res.data.data);
      Toast("登录成功");
      this.$router.push("/");
    },
    login() {},
    onClickLeft() {},
    onClickRight() {
      this.useCode = !this.useCode;
    },
    getCode() {},
  },
};
</script>

<style lang="less" scoped>
// .main-box {
//   display: flex;
//   // flex-direction: column;
//   // justify-content: center;
//   // align-items: center;
//   padding: 10px;
// }
.logo {
  height: 100px;
  width: 100%;
  background: url("@/assets/logo1.png") no-repeat center / 120px auto;
  background-size: 75% 100%;
}
// .login-form {
// }
</style>